October 2020
A general ggplot template
ggplot(data = ..., aes(x = ..., y = ..., ...))+
geom_*()+ add geometrical objects (points, lines, areas, …)
facet_*()+ split in subplots
coord_*()+ changes the coordinate system
scale_*_*()+ changes the way geoms look
theme_*() changes the look & feel of the plot
base <- iris %>%
ggplot(aes(x = Sepal.Length, y = Sepal.Width,
color = Species, size = Petal.Width))+
geom_point()
base
coord_*functions change the behavior / scale of the axes
base + coord_flip()
change the limits of the plot to ‘zoom’ on a part of it (!!handle with care!!)
base + coord_cartesian(xlim = c(5, 6), ylim = c(3,4))
sometimes you need the two scales on the two axes to be comparable
ggplot(mpg, aes(cty, hwy)) + geom_point() + labs(title = "It looks like mileage in City and Highway is the same!")
sometimes you need the two scales on the two axes to be comparable
ggplot(mpg, aes(cty, hwy)) + geom_point() + coord_fixed() + labs(title = "But mileage is higher in highways!")
sometimes you need axes to be n an exact proportion
ggplot(mpg, aes(cty, hwy)) + geom_point() + coord_fixed(ratio = 1/5) + labs(title = "City is 5 times as big as highway here")
log scales
ggplot(mpg, aes(cty, hwy)) + geom_point() + coord_trans(x = "log10", y = "log10")+ labs(title = "log axes")
we can also transform a (x,y) coordinate into a (distance, angle)
base + coord_polar()
this is the only way to make a pie chart in ggplot: a bar chart in polar coordinates.
ggplot(mpg, aes(x = "", y = class, fill = class)) + geom_col()
this is the only way to make a pie chart in ggplot: a bar chart in polar coordinates.
ggplot(mpg, aes(x = "", y = class, fill = class)) + geom_col() + coord_polar("y", start = 0)
scale_*_*functions change the appearance of the mapping
scale_NAME_TYPENAME can take values: alpha, color, fill, linetype, shape, size, x, yTYPE can take different values according to the scale typemake our base plot greyscale and change the size mapping
base + scale_color_grey() + scale_size_continuous(range = c(1, 10))
scale_color_*Manipulating colors is fun! but you need to pay attention to some stuff
Brewer palettes
base + scale_color_brewer()
Brewer palettes
base + scale_color_brewer(type = "qual")
Brewer has many palettes
base + scale_color_brewer(palette = "Set1")
Viridis palette
base + scale_color_viridis_d()
RColorBrewer::display.brewer.all()
manual scalesbase + scale_color_manual(values = c("pink", "violet", "brown"))
https://www.datanovia.com/en/blog/awesome-list-of-657-r-color-names/
a simple plot to showcase stuff
base2 <- ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Petal.Width))+ geom_point() base2
change the gradient
base2 + scale_color_gradient(low = "green", high = "red")
a gradient with an intermediate step
base2 + scale_color_gradient2(low = "red", mid = "grey", high = "green", midpoint = mean(iris$Petal.Width))
Sometimes you have continuous data but you want to treat it as discrete:
binnedscales
base2 + scale_color_binned()
change the gradient to be colorblind proof
base2 + scale_color_viridis_c()
base + scale_size_continuous(range = c(1,12))
base + scale_size_binned(n.breaks = 3, range = c(1,12))
in barplots, the relevant variable is the fill
ggplot(mpg, aes(x = manufacturer, color = manufacturer)) + geom_bar()
in barplots, the relevant variable is the fill
prod <- ggplot(mpg, aes(x = manufacturer, fill = manufacturer)) + geom_bar() prod
prod + scale_fill_viridis_d()
if appearance does not vary then pass it directly:
ggplot(mpg, aes(x = manufacturer)) + geom_bar(fill = "red")
themefunctions refer to the appearance of everything but the mapping
theme_minimal())ggthemes, hrbrthemes)theme()labs()base + theme_classic()
base + theme_dark()
base + theme_linedraw()
base + theme_void()
install.packages("ggthemes")
library(ggthemes)
base + theme_excel_new()
library(hrbrthemes) base + theme_ipsum_rc()
theme()base + theme(legend.position = "left")
theme()base + theme(panel.background = element_rect(fill = "yellow"))
base + labs(title = "title", subtitle = "subtitle", caption = "CAPTION", tag = "tag")
to save a plot to file, use
ggsave(). By default it saves the last plot made
ggsave(filename, width, height, dpi)
sometimes you need to add some lements that are not in the data
there are three types of lines:
base + geom_hline(yintercept = 3, color = "red")
base + geom_vline(xintercept = 5, color = "blue")
base + geom_abline(slope = 0.8, intercept = -2, color = "orange")
geomthere are two text geoms:
textandlabel
carspeed <- mpg %>% filter(year == 2008 & trans == "manual(m5)") %>% ggplot(aes(x = cty, y = hwy)) carspeed + geom_point()
geomlabeling cars by their name: text
carspeed + geom_text(aes(label = model))
geomlabeling cars by their name: label
carspeed + geom_label(aes(label = model))
just put a custom text somewhere
base + annotate(geom = "text", label = "ANNOTATION", x = 6, y = 4, hjust = 0)